//
//  HJStatusRetweekView.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/11.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SnapKit

class HJStatusRetweetView: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusViewModel:HJStatusViewModel?{
        
        didSet{
            
            contentLabel.text = statusViewModel?.retweetText
            
            
            self.bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
                pictureView.pic_urls = pic_urls
                
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
                })
                
            }else{
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HJStatusCellMargin).constraint
                })
            }
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HJStatusCellMargin)
            make.top.equalTo(self).offset(HJStatusCellMargin)
        }
        
        //图片
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HJStatusCellMargin)
            make.leading.equalTo(contentLabel)
            
            
        }
        
        // 当前控件的高与内容的高度一样的
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
        }

        
    }
    
    // MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2 * HJStatusCellMargin)

    private lazy var pictureView: HJStatusPictureView = {
        
        let view = HJStatusPictureView()
        //view.backgroundColor = UIColor(white: 0.95, alpha: 1)
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
